﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SGArchiveSystem7xDB.Linq
{
    public class T_LogArchiveInitDal
    {
        static SGArchiveSystem7xEntities conn;

        /// <summary>
        /// 查询档案日志
        /// </summary>
        /// <returns></returns>
        public static List<T_LogArchiveInit> Get_LogArchiveInit(out int count , string DH ,string  starttime, string endtime, string uname, string CZR,  int type,string CZLX = "全部", int start = -1, int row = -1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_LogArchiveInit.Select(c => c);
                if (type != -1)
                {
                    query = query.Where(c => c.LOG_ARID == type);
                }
                if (DH != null && DH != "")
                {
                    query = query.Where(c => c.LOG_ANAME.Contains(DH));
                }
                if (starttime != null && starttime != "")
                {
                    DateTime st = DateTime.Parse(starttime);
                    query = query.Where(c => c.LOG_DATE >= st);
                }
                if (endtime != null && endtime != "")
                {
                    DateTime et = DateTime.Parse(endtime);
                    query = query.Where(c => c.LOG_DATE <= et);
                }
                if (CZR != null && CZR != "")
                {
                    query = query.Where(c => c.LOG_UNAME.Contains(CZR));
                }
                if (CZLX != "全部" && CZLX != null)
                {
                    query = query.Where(c => c.LOG_TYPE == CZLX);
                }

                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderByDescending(c => c.LOG_DATE).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.OrderByDescending(c => c.LOG_DATE).ToList();
                    count = list.Count;
                    return list;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static _ReturnValue Add_Log(T_LogArchiveInit en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                conn.T_LogArchiveInit.Add(en);
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }
    }
}
